<?php
/**
 * @title 330. 按要求补齐数组
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $n
     * @return Integer
     */
    function minPatches($nums, $n) {
        /**
         * @since 2020-12-29 倒序
         */
        rsort($nums);

        /**
         * @since 2020-12-29 各种组合
         */
        $sums = self::find($nums, [0=>1], $n);

        /**
         * @since 2020-12-29 缺哪个数字
         */
        $maynums = [];
        if(count($sums) <= $n){
            for($i=1; $i<=$n; $i++){
                if(!isset($sums[$i])){
                    for($j=1; $j<=$i; $j++){
                        $maynums[] = $j;
                    }
                    break;
                } else {}
            }
        } else {}

        return $maynums;
    }

    /**
     * @title 加一个数
     * @author start2004
     * @since 2020-12-29 Created
     *
     * @param int $num
     * @param array $sums
     * @param int $max
     * @return array
     */
    function combination($num = 0, $sums = [], $max = 0){
        /**
         * @since 2020-12-29 依次加上, sums不一定是升序
         */
        foreach ($sums as $sum=>$val){
            $sum += $num;
            if($sum <= $max){
                $sums[$sum] = 1;
            } else {}
        }

        /**
         * @return
         */
        return $sums;
    }

    /**
     * @title 获得数字组合之和
     * @author start2004
     * @since 2020-12-29 Created
     *
     * @param array $nums
     * @param array $sums
     * @param int $max
     * @return array|mixed
     */
    function find($nums = [], $sums = [], $max = 0){
        $num = array_pop($nums);

        /**
         * @since 2020-12-29 依次加上, sums不一定是升序
         */
        foreach ($sums as $sum=>$val){
            $sum += $num;
            if($sum <= $max){
                $sums[$sum] = 1;
            } else {}
        }

        /**
         * @since 2020-12-29 非空继续
         */
        if($nums){
            $sums = self::find($nums, $sums, $max);
        } else {}

        /**
         * @return
         */
        return $sums;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202012/2020.12.29.php
 */
$datas = [
    // [[1,3], 6], // 1
    [[1,5,10], 20], // 2
    // [[1,2,2], 5], // 0
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->minPatches(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
